package service

import (
	"encoding/json"
	"fmt"
	"nt-order/common"
	"nt-order/conf"
	"nt-order/model"
	"time"
)

type UserDataPermission struct {
}

func (that *UserDataPermission) GetUserPermissionByToken(token string) (*model.UserInfo, error) {
	redisKey := common.GetCacheKey(fmt.Sprintf("ACCOUNT_INFO:%s", token))
	redis := conf.GetRedis()
	data, err := redis.Get(redisKey)
	if err != nil {
		center := StaffIdFromPermissionCenter{Token: token}
		staffId := center.UserStaffid()
		info := new(model.OrderStaffList).UserInfo(staffId)
		userInfo := model.UserInfo{}
		userInfo.Staff_name = info.Staff_name
		userInfo.Staff_id = info.Staff_id
		userInfo.Data_permission = []int{}

		m, err := json.Marshal(userInfo)
		if err != nil {
			panic(err)
		}
		data=string(m)

		err = redis.Set(redisKey, data, 604800*time.Second)
		if err != nil {
			panic(err)
		}
	}

	userInfo := new(model.UserInfo)
	err = json.Unmarshal([]byte(data), userInfo)
	if err != nil {
		return nil, err
	}

	return userInfo, nil
}
